package com.gsoe.problem.listen;


import com.gsoe.base.exception.JudgeException;
import com.gsoe.base.model.MqMessage;
import com.gsoe.base.model.Result;
import com.gsoe.problem.client.JudgeClient;
import com.gsoe.problem.dto.SubmitDTO;
import com.gsoe.problem.dto.UpdateScoreDTO;
import com.gsoe.problem.service.ExamRegisterUserService;
import com.gsoe.problem.service.TestService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MqListen {
    @Autowired
    TestService testService;
    @Autowired
    ExamRegisterUserService examRegisterUserService;

    @Autowired
    JudgeClient judgeClient;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "submit.queue1",durable = "true"),
            exchange = @Exchange(name = "test.direct",type = ExchangeTypes.DIRECT,delayed = "true"),
            key={"TextSubmit"}
    ))
    public void listen1(SubmitDTO submitDTO) {
        Result<Map<Long, String>> testCache = judgeClient.getTestCache(submitDTO.getTextId());
        if(testCache.getCode()!=200){
            throw new JudgeException("listen1:获取缓存失败");
        }
        submitDTO.setInputMap(testCache.getData());
        testService.submit(submitDTO);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="updateScore.queue1"),
            exchange= @Exchange(name="exam.direct",type = ExchangeTypes.DIRECT),
            key = {"updateScore"}
    ))
    public void listen2(UpdateScoreDTO updateScoreDTO) {
        examRegisterUserService.updateScore(updateScoreDTO);
    }
}
